package com.bqtv.sport.ui.mine

import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.Toast
import com.bqtv.sport.R
import com.bqtv.sport.app.AppActivity
import com.bqtv.sport.commen.dialog.MessageDialog
import com.bqtv.sport.commen.utils.FileUtils
import com.bqtv.sport.databinding.ActivitySettingBinding
import com.hjq.base.BaseDialog

class SettingActivity : AppActivity<ActivitySettingBinding>() {
    companion object {
        fun start(context: Context) {
            val intent = Intent(context, SettingActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun getViewBinding() = ActivitySettingBinding.inflate(layoutInflater)

    override fun initView() {

    }

    override fun initData() {
        binding.tvCache.text = (FileUtils.getCacheSize(this) / 1024 / 1024).toString() + "M"


        initListener()
    }

    private fun initListener() {
        setOnClickListener(
            binding.llClearCache,
            binding.llSetLoginPsw,
            binding.llPayPsw,
            binding.llLogoutAccount,
            binding.llPrivacyPolicy,
            binding.llServicePolicy
        )
    }

    override fun onClick(view: View) {
        super.onClick(view)
        when (view) {
            binding.llClearCache -> {
                var cacheDialog= MessageDialog.Builder(this)
                    .setTitle(R.string.kind_reminder)
                    .setMessage(R.string.clear_cache_tip)
                    .setListener(object : MessageDialog.OnListener {
                        override fun onConfirm(dialog: BaseDialog?) {
                            clearAppCache()
                        }

                    })
                cacheDialog.show()

            }
            binding.llSetLoginPsw -> {
                EditPasswordActivity.start( this)
            }
            binding.llPayPsw -> {
                SetPayPswActivity.start( this)
            }
            binding.llLogoutAccount -> {
                HtmlActivity.start(this,1)
            }
            binding.llPrivacyPolicy -> {
                HtmlActivity.start(this,3)

            }
            binding.llServicePolicy -> {
                HtmlActivity.start(this,2)

            }

        }
    }

    fun clearAppCache() {
        try {
            /*// 清理内部缓存(/data/data/<应用包名>/cache)
            var file = this.cacheDir
            deleteDir(file)

            // 清理内部文件(/data/data/<应用包名>/files)
            file = this.filesDir
            deleteDir(file)

            // 清理外部缓存(/storage/emulated/0/Android/data/<应用包名>/cache)
            file = this.externalCacheDir
            deleteDir(file)

            // 清理外部文件(/storage/emulated/0/Android/data/<应用包名>/files)
            file = getExternalFilesDir(null)
            deleteDir(file)

            // 清理数据库
            SQLiteDatabase.deleteDatabase(File(this.filesDir, "your_database_name"))*/
            FileUtils.clearCache(this)
            binding.tvCache.text = (FileUtils.getCacheSize(this) / 1024 / 1024).toString() + "M"

            Toast.makeText(this, "缓存已清理", Toast.LENGTH_SHORT).show()
        } catch (e: Exception) {
            e.printStackTrace()
            Toast.makeText(this, "清理缓存失败", Toast.LENGTH_SHORT).show()
        }
    }
}